/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "vibrate.h"
#include "cJSON.h"
#include "FileCache.h"
#include <fcntl.h>
#include <sensors/vibrator.h>
#include <sys/stat.h>

REGISTER_PLUGIN_CLASS(vibrate)
bool vibrate::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "startVibrate") {
        int nSize = cJSON_GetArraySize(args);
        if(nSize <= 0)
            return false;
         cJSON* pCont = cJSON_GetArrayItem(args, 0);
        if(pCont->type != cJSON_Number) {
            cbc.error("json type Error");
            return false;
        }
        int nNumber = pCont->valueint;
        Vibrator_Attribute vibrateAttribute = {
            .usage = VIBRATOR_USAGE_RING
        };
        int nRet = OH_Vibrator_PlayVibration(nNumber, vibrateAttribute);
        if(nRet != 0) {
            cbc.error(nRet);
            return false;
        }
        cbc.success();
    } else if(action == "customVibrate") {
        int nSize = cJSON_GetArraySize(args);
        if(nSize <= 0)
            return false;
        string strFileTmp = Application::g_databaseDir.substr(0, Application::g_databaseDir.find("database")-1)+"/base/temp/vibrate.json";
        cJSON* pCont = cJSON_GetArrayItem(args, 0);
        if(pCont->type != cJSON_Object) {
            cbc.error("json type Error");
            return false;
        }
        
        const char * pBuf = cJSON_Print(pCont);
        int nFileSize = FileCache::writeFile(strFileTmp, pBuf, strlen(pBuf));
        if(nFileSize != strlen(pBuf)) {
            free((void*)pBuf);
            cbc.error("write file error");
            return false;
        }
        free((void*)pBuf);
        
        int32_t fd = open(strFileTmp.c_str(), O_RDONLY);
        struct stat64 statbuf = { 0 };
        if (fstat64(fd, &statbuf) == 0) {
            Vibrator_FileDescription fileDescription = {
                .fd = fd,
                .offset = 0,
                .length = statbuf.st_size
            };
            Vibrator_Attribute vibrateAttribute = {
                .usage = VIBRATOR_USAGE_RING
            };
            int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute); // 播放自定义振动序列。
            if(ret != 0) {
                close(fd);
                cbc.error(ret);
                return false;
            }
        }
        
       close(fd);
       cbc.success();
    } else if(action == "cancelVibrate") {
        int nRet = OH_Vibrator_Cancel();
        if(nRet != 0) {
            cbc.error(nRet);
            return false;
        }
        cbc.success();
    }
    return  true;
}